$(function() {
    if (localStorage.userName == "null" || localStorage.userName == undefined || localStorage.userName == "") {
        alert("请先登录");
        window.location.href = "login.html";
    }else{
        if(localStorage.idNum=="null" || localStorage.idNum==undefined ||  localStorage.idNum==""){
            alert("请先完善个人信息");
            window.location.href="personinfo.html";
        }
    }

    if (typeof(localStorage.xftype) !== "undefined") {
        newgetjycxinfo();
    } else {
        getjycxinfo();
       
    }


    $("body").on("click", ".xfxx-card", function() {
        window.localStorage.xflx = $(this).attr("xflx");
        window.localStorage.cjsj = $(this).attr("cjsj");
        window.localStorage.jdh = $(this).attr("jdh");
        window.localStorage.jyje = $(this).attr("jyje");
        window.localStorage.xfgs = $(this).attr("xfgs");
        window.location.href = "jyxq.html";
    })

});

function getjycxinfo() {
    $.ajax({
        url: requestUrl + "app/queryPersonalCardInfoAndConsumeRecordsByPaperNo.do",
        type: "POST",
        data: { appFlag:1,citizenCardNo: GetQueryString("name"), token: sessionStorage.token, userId: localStorage.userId,transactionType:"0"},
        dataType: "JSON",
        success: function(json) {
            if (json.code == 0) {

                $(".jycx-name").text(json.data[0].appName);
                $(".money").text(json.data[0].newBalance);
                if (json.data[0].transactionRecords.length !== 0) {
                    var zsr = 0;
                    var zzc = 0;
                    $(json.data[0].transactionRecords).each(function(i, o) {
                        if (o.acTypeName == "充值") {
                            zsr = zsr + o.txnAmt;
                            var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='+" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>+" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                        } else {
                            zzc = zzc + o.txnAmt;
                            var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='-" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>-" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                        }
                    })
                    $(".zsr").text("充值：￥" + zsr);
                    $(".zcmoney").text("消费：￥" + zzc);
                } else {
                    var wu = $("<div style='background-color: #f0eff4;text-align: center;padding: 18px 0 0 0;color: #999999;'>暂无消费记录！</div>").appendTo(".xfxx");
                }
            } else {
                if (json.message == "用户尚未登陆") {
                    window.location.href = "login.html";
                } else {
                    alert(json.message);
                }
            }
        }
    })
}

function newgetjycxinfo() {
    if (typeof(localStorage.xfyear) !== "undefined") {
        $(".cxny").text(localStorage.xfyear + "年" + localStorage.xfmonth + "月")
    }
    $.ajax({
        url: requestUrl + "app/queryPersonalCardInfoAndConsumeRecordsByPaperNo.do",
        type: "POST",
        data: { appFlag:localStorage.appFlag,citizenCardNo: GetQueryString("name"), year: localStorage.xfyear, month: localStorage.xfmonth,userId:localStorage.userId,token:sessionStorage.token,transactionType:localStorage.xftyype},
        dataType: "JSON",
        success: function(json) {
            if (json.code == 0) {
                $(".jycx-name").text(json.data[0].appName   );
                $(".money").text(json.data[0].newBalance);
                if (json.data[0].transactionRecords.length !== 0) {
                    var zsr = 0;
                    var zzc = 0;
                    if (localStorage.xftype == "全部") {
                        var zsr = 0;
                        var zzc = 0;
                        $(json.data[0].transactionRecords).each(function(i, o) {
                            if (o.acTypeName == "充值") {
                                zsr = zsr + o.txnAmt;
                                var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='+" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>+" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                            } else {
                                zzc = zzc + o.txnAmt;
                                var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='-" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>-" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                            }
                        })
                        $(".zsr").text("充值：￥" + zsr);
                        $(".zcmoney").text("消费：￥" + zzc);
                    } else {
                        $(json.data[0].transactionRecords).each(function(i, o) {
                            if (o.acTypeName == localStorage.xftype) {
                                if (o.acTypeName == "充值") {
                                    zsr = zsr + o.txnAmt;
                                    var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='+" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>+" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                                    $(".zcmoney").css("display", "none");
                                } else if (o.acTypeName == "消费") {
                                    zzc = zzc + o.txnAmt;
                                    var xfjv = $("<div class='xfxx-card' xflx='" + o.acTypeName + "' cjsj='" + o.creDate + "' jdh='" + o.orderNo + "' jyje='-" + o.txnAmt + "' xfgs='" + o.operatCompany + "'><div class='card-div card-img'><img src='images/jycxlogo.png'></div><div class='card-div card-nr'><div class='xfxx-xx'><span>" + o.acTypeName + "</span><span class='xfxx-money'>-" + o.txnAmt + "</span></div><div class='xfxx-time'><span>" + o.creDate + "</span></div></div></div>").appendTo(".xfxx");
                                    $(".zsr").css("display", "none");
                                } else {

                                }
                            }
                        })
                    }
                    $(".zsr").text("充值：￥" + zsr);
                    $(".zcmoney").text("消费：￥" + zzc);
                } else {
                    var wu = $("<div style='background-color: #f0eff4;text-align: center;padding: 18px 0 0 0;color: #999999;'>暂无消费记录！</div>").appendTo(".xfxx");
                }
            } else {
                if (json.message == "用户尚未登陆") {
                    window.location.href = "login.html";
                } else {
                    alert(json.message);
                      localStorage.removeItem("xftype");
            localStorage.removeItem("xfyear");
            localStorage.removeItem("xfmonth");
            localStorage.removeItem("appFlag");
            localStorage.removeItem("xftyype");
            window.location.reload()
                }
            }
            localStorage.removeItem("xftype");
            localStorage.removeItem("xfyear");
            localStorage.removeItem("xfmonth");
            localStorage.removeItem("appFlag");
            localStorage.removeItem("xftyype");
        }
    })
}

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}